#ifndef __QyGeoEdit__
#define __QyGeoEdit__

#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsView>
#include <QtGui/QPolygonF>

class QAbstractGraphicsShapeItem;
class QGraphicsEllipseItem;
class QGraphicsPixmapItem ;

namespace QyMath {

class Geometry : public QPolygonF {
public:
    Geometry() : p_isCircle(false), p_radius(0.0) {}
    virtual ~Geometry() {}

    bool isCircle() const { return p_isCircle; }
    qreal radius() const { return p_radius; }

    void setRadius( qreal radius ) { p_radius = radius; p_isCircle = true; }
    void dropCircleFlag() { p_isCircle = false; }

    QPointF center;
    QList<Geometry> subGeometry;

private:
    bool p_isCircle;
    qreal p_radius;

};

};

class QyGeoEdit : public QGraphicsView {
    Q_OBJECT
public:
    enum EditMode {
        DefaultMode, DrawCircleMode, DrawPolygonMode
    };
    explicit QyGeoEdit( QWidget * parent = 0 );
    virtual ~QyGeoEdit();

    QyMath::Geometry geo() const { return p_geo; }
    QyGeoEdit::EditMode editMode() const { return p_mode; }
    QPointF calcLocal( const QPointF & pos ) const { return rootItem ? pos - p_geo.center : pos; }
	QPointF adjustPos( const QPointF & pos );

    void setGeo( const QyMath::Geometry & geo );
    void addGeo( const QyMath::Geometry & geo );

public slots:
    void initDefaultMode();
    void initDrawCircleMode();
    void initDrawPolygonMode();
    void setEditMode( QyGeoEdit::EditMode editMode );
    void showEditPanel();
    void clear();
    bool openSubstrateImage( const QString & filePath = QString() );
    void zoomIn();
    void zoomOut();

private:
    QAbstractGraphicsShapeItem * addGeoToScene( QyMath::Geometry & geo );
    void mouseMoveEvent( QMouseEvent * event );
    void mousePressEvent( QMouseEvent * event );
    void mouseReleaseEvent( QMouseEvent * event );

    QyMath::Geometry p_geo;
    QGraphicsScene scene;
    QGraphicsItem * rootItem;
    EditMode p_mode;
    QPointF p_pressPos;
    QGraphicsEllipseItem * p_newCircle;
    QGraphicsPolygonItem * p_newPolygon;
    bool p_mousePressed;
    QWidget * p_editPanel;
    int lastPolygonPoint;
    QGraphicsPixmapItem * substrateImage;
    qreal scale,
        editItemSize;

};


#endif//__QyGeoEdit__
